﻿using Pay.DTO;
using Pay.TO;
using Pay.IService;
using System.Web.Mvc;
using Pay.Core;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("setting")]
    public class SettingController : AdminController
    {
        private ISettingService _iSettingService;
        private IMerchantService _iMerchantService;

        public SettingController(ISettingService iSettingService, IMerchantService iMerchantService)
        {
            _iSettingService = iSettingService;
            _iMerchantService = iMerchantService;
        }

        public JsonResult GetSetting(string _code)
        {
            var result = _iSettingService.Get();
            switch (_code.ToLower())
            {
                case "site":
                    return Json(ResultToJson.Json(result.site), JsonRequestBehavior.AllowGet);
            }

            return Json(ResultToJson.Json(0, "失败", ""), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [ValidateInput(false)]
        public JsonResult PutSetting(string _code)
        {
            var setting = _iSettingService.Get();
            switch (_code.ToLower())
            {

                case "site":
                    var site = new site_setting();
                    UpdateModel(site);

                    var file_url = Upload("setting");
                    if (!string.IsNullOrEmpty(file_url))
                        site.logo = file_url;

                    setting.site = site;
                    break;
            }

            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
        }


        //return Json(ResultToJson.Json(_iPaymentService.Get()), JsonRequestBehavior.AllowGet);

        #region 消息启用配置
        //[Route("send_conetnt_setting")]
        //public JsonResult SendConentSetting(int type) {
        //    var settings = _iSettingService.Get().Where(a => a.send_type.Contains(type)).OrderBy(a => a.sort);
        //    var codes = new List<string> { };

        //    var setting = _iSettingService.Get();
        //    switch (type) {
        //        case 1://APP消息
        //            codes = setting.app.send_content;
        //            break;
        //        case 2://短信
        //            codes = setting.sms.send_content;
        //            break;
        //        case 3://微信
        //            codes = setting.weixin.send_content;
        //            break;
        //    }

        //    return Json(ResultToJson.Json(settings.Select(a => new {
        //        a.name,
        //        a.content,
        //        a.code,
        //        check = codes.Contains(a.code)
        //    })), JsonRequestBehavior.AllowGet);
        //}

        //[Route("save_send_conetnt_setting")]
        //public JsonResult SaveSendContentSetting(int type, List<string> codes) {
        //    codes = (codes == null ? new List<string> { } : codes);
        //    var setting = _iSettingService.Get();
        //    switch (type) {
        //        case 1://APP消息
        //            setting.app.send_content = codes;
        //            break;
        //        case 2://短信
        //            setting.sms.send_content = codes;
        //            break;
        //        case 3://微信
        //            setting.weixin.send_content = codes;
        //            break;
        //    }

        //    var result = _iSettingService.Save(setting);

        //    return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
        //}
        #endregion

        #region 拓垦基地管理
        public JsonResult GetTokenSetting()
        {
            var model = _iSettingService.GetTokenSetting().token;
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SaveTokenSetting(token_setting model) {
            var setting = _iSettingService.GetTokenSetting();
            setting.token = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 拓垦App配置
        /// <summary>
        /// 获取拓垦app配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenAppSetting()
        {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null)
            {
                return Json(ResultToJson.Json(setting.app), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存拓垦app设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveTokenAppSetting(token_app_setting model)
        {
            var setting = _iSettingService.GetTokenSetting();
            setting.app = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 拓垦短信配置
        /// <summary>
        /// 获取拓垦短信配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenSMSSetting()
        {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null)
            {
                return Json(ResultToJson.Json(setting.sms), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存拓垦短信设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveTokenSMSSetting(token_sms_setting model)
        {
            var setting = _iSettingService.GetTokenSetting();
            setting.sms = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 拓垦红钻转赠配置
        /// <summary>
        /// 拓垦红钻转赠配置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenTransferSetting() {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null) {
                //转换成百分比
                setting.token_transfer.transfer_fee_rate = setting.token_transfer.transfer_fee_rate * 100;
                setting.token_transfer.first_user_benefit_rate = setting.token_transfer.first_user_benefit_rate * 100;
                setting.token_transfer.second_user_benefit_rate = setting.token_transfer.second_user_benefit_rate * 100;
                return Json(ResultToJson.Json(setting.token_transfer), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存红钻转赠配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveTokenTransferSetting(token_transfer_setting model) {
            var setting = _iSettingService.GetTokenSetting();
            if (model.transfer_fee_rate < 0)
                throw new ServerErrorException("赠送资产手续费比例不能小于0");
            if (model.first_user_benefit_rate < 0)
                throw new ServerErrorException("赠送资产一级邀请人分润比例不能小于0");
            if (model.second_user_benefit_rate < 0)
                throw new ServerErrorException("赠送资产二级邀请人分润比例不能小于0");
            if (model.first_user_benefit_rate + model.second_user_benefit_rate > 100)
                throw new ServerErrorException("一二级分润比例不能超过100%");
            //单位转换
            model.transfer_fee_rate = model.transfer_fee_rate / 100M;
            model.first_user_benefit_rate = model.first_user_benefit_rate / 100M;
            model.second_user_benefit_rate = model.second_user_benefit_rate / 100M;

            setting.token_transfer = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 拓垦创建测试（运营）用户
        /// <summary>
        /// 拓垦创建测试（运营）用户试图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CreateTokenTestUserView()
        {
            return Json(ResultToJson.Json(new { test_mobie_prefix = t_RequestMethod.TestMobiePrefix, mobileSuffix = ""}), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult CreateTokenTestUser(string mobileSuffix)
        {
            var result = _iMerchantService.CreateTokenTestUser(mobileSuffix);

            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 拓垦提现设置
        /// <summary>
        /// 获取提现设置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenWithdrawSetting() {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null) {
                return Json(ResultToJson.Json(setting.token_base_setting), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存提现设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult saveTokenWithdrawSetting(token_base_setting model) {
            var setting = _iSettingService.GetTokenSetting();
            setting.token_base_setting = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 拓垦商城购物设置
        /// <summary>
        /// 获取拓垦商城购物设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenMallShoppingSetting() {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null) {
                return Json(ResultToJson.Json(setting.token_mall_shopping_setting), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存拓垦商城购物设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveTokenMallShoppingSetting(token_mall_shopping_setting model) {
            if (model.give_horse_power_1 < 0 || model.give_horse_power_2 < 0 || model.give_horse_power_3 < 0 || model.give_horse_power_4 < 0 || model.give_horse_power_5 < 0)
                throw new ServerErrorException("增加马力值不能小于0");
            var setting = _iSettingService.GetTokenSetting();
            setting.token_mall_shopping_setting = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 购买商城红钻数设置
        /// <summary>
        /// 获取购买商城红钻数设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTokenOpenSupplierSetting() {
            var setting = _iSettingService.GetTokenSetting();
            if (setting != null) {
                return Json(ResultToJson.Json(setting.token_open_supplier_setting), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<setting_dto>("获取配置失败"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存购买商城红钻数设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveTokenOpenSupplierSetting(token_open_supplier_setting model) {
            if (model.open_supplier_red_drill <= 0)
                throw new ServerErrorException("购买节点商城所需红钻数必须大于0");
            var setting = _iSettingService.GetTokenSetting();
            setting.token_open_supplier_setting = model;
            var result = _iSettingService.Save(setting);
            return Json(ResultToJson.Json(result >= 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}